package com.eascs.dubbo.api.internal;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.common.vo.ModelResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.dao.CustomerInfoDao;
import com.eascs.crm.nsEditDetail.entity.CustomerBasicEntity;
import com.eascs.crm.nsEditDetail.service.CustomerInfoService;
import com.eascs.crm.nsEditDetail.service.InternalBorrowerInfoService;
import com.eascs.crm.nsEditDetail.vo.CustomerBasicVo;
import com.eascs.crm.nsEditDetail.vo.O2oLoanUserVo;

@Service
public class InternalBorrowerInfoServiceImpl implements InternalBorrowerInfoService {

	private final Logger logger = LoggerFactory.getLogger(InternalBorrowerInfoServiceImpl.class);

	@Autowired
	CustomerInfoService customerInfoService;

	@Autowired
	CustomerInfoDao customerDao;

	@Override
	public CustomerBasicVo queryCustomerInfo(String loanUserId, String spouseId) {
		if (StringUtils.isBlank(loanUserId)) {
			return null;
		}
		CustomerBasicVo customer = customerDao.queryCustomerById(loanUserId);
		if (StringUtils.isBlank(spouseId)) {
			return customer;
		}
		CustomerBasicVo spouse = customerDao.queryCustomerById(spouseId);
		if (customer == null) {
			customer = new CustomerBasicVo();
		}
		customer.setSpouse(spouse);
		return customer;
	}

	@Override
	public String saveBorrowerInfo(CustomerBasicEntity customerBasicEntity, O2oLoanUserVo o2oLoanUserVo) {
		if (StringUtil.isBlank(o2oLoanUserVo.getSurnamePinyin())) {
			return "借款人/配偶姓拼音不能为空";
		}
		if (StringUtil.isBlank(o2oLoanUserVo.getNamePinyin())) {
			return "借款人/配偶名拼音不能为空";
		}
		if (StringUtil.isBlank(o2oLoanUserVo.getUserPhone())) {
			return "借款人/配偶联系电话不能为空";
		}
		if(StringUtil.isEmpty(customerBasicEntity.getMarital_status())){
			if("2".equals(customerBasicEntity.getMarital_status())){
				if(StringUtil.isEmpty(customerBasicEntity.getMarryDate())){
					return "结婚日期不能为空";
				}
			}else if("3".equals(customerBasicEntity.getMarital_status())){
				if(StringUtil.isEmpty(customerBasicEntity.getDivorceDate())){
					return "离婚日期不能为空";
				}
			}
		}
		logger.info("输入借款人姓拼音：【{}】", o2oLoanUserVo.getSurnamePinyin());
		logger.info("输入借款人名拼音：【{}】", o2oLoanUserVo.getNamePinyin());
		ModelResult<CustomerBasicEntity> modelResult = customerInfoService.udpateEntity(customerBasicEntity, o2oLoanUserVo);
		if (!modelResult.isSuccess()) {
			return "修改失败";
		}
		return null;
	}

}
